﻿//创建时间: 2010-10-14
//创建作者: 李 城
//功能说明: 可选值排序
//最后修改: 2010-10-14
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Web.Admin
{
    public partial class FormInputValue_save_sort : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.SystemManage_ContentModel;
        }
        #endregion


        #region 页面变量
        protected int m_id = 0;
        protected string m_Title = "";
        protected FormInputValueInfo m_FormInputValueInfo = new FormInputValueInfo();
        protected FormInputValueManage m_FormInputValueManage = new FormInputValueManage();
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
                BindData();
            }
        }
        /// <summary>
        /// 上移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnup_Click(object sender, EventArgs e)
        {
            if (lbfiled.SelectedIndex == 0)
                return;
            else
            {
                int index = lbfiled.SelectedIndex;
                SetOrder(m_FormInputValueManage.GetInfoById(lbfiled.SelectedValue.ToInt()),
                         m_FormInputValueManage.GetInfoById(lbfiled.Items[index - 1].Value.ToInt()));
                BindData();
                lbfiled.Items[index - 1].Selected = true;
            }
        }
        /// <summary>
        /// 下移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btndown_Click(object sender, EventArgs e)
        {
            if (lbfiled.SelectedIndex >= lbfiled.Items.Count - 1)
                return;
            else
            {
                int index = lbfiled.SelectedIndex;
                SetOrder(m_FormInputValueManage.GetInfoById(lbfiled.SelectedValue.ToInt()),
                         m_FormInputValueManage.GetInfoById(lbfiled.Items[index + 1].Value.ToInt()));
                BindData();
                lbfiled.Items[index + 1].Selected = true;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ExecuteJs("parent.reset();");
        }
        #endregion

        #region 页面方法

        public void LoadBase()
        {
            int.TryParse(InitRequestViewState("id"), out m_id);
            m_FormInputValueInfo = m_FormInputValueManage.GetInfoById(m_id);
            if (m_id <= 0 || m_FormInputValueInfo == null)
            {
                Alert("请通过正确的途径进入本页面");
                return;
            }
            else
            {
                m_Title = m_FormInputValueInfo.Name + "";
                InitPageViewState("modelinfo", m_FormInputValueInfo);
            }
        }

        public void BindData()
        {
            int.TryParse(GetRequestViewState("id"), out m_id);
            m_FormInputValueInfo = (FormInputValueInfo)GetPageViewState("modelinfo");
            string condation = string.Format(" AND FormInputValueTypeId={0} AND ParentId={1} ",m_FormInputValueInfo.FormInputValueTypeId,m_FormInputValueInfo.ParentId);
            lbfiled.DataSource = m_FormInputValueManage.GetListByCondition(0, condation, " DisplayOrder ");
            lbfiled.DataTextField = "Name";
            lbfiled.DataValueField = "FormInputValueId";
            lbfiled.DataBind();
        }
        public void SetOrder(FormInputValueInfo oinfo1, FormInputValueInfo oinfo2)
        {
            if (oinfo1 != null && oinfo2 != null)
            {
                int tempOrder = oinfo1.DisplayOrder;
                oinfo1.DisplayOrder = oinfo2.DisplayOrder;
                oinfo2.DisplayOrder = tempOrder;
                m_FormInputValueManage.Save(oinfo1);
                m_FormInputValueManage.Save(oinfo2);
            }
        }
        #endregion
    }
}
